package com.hmyd.hmyd_android.adapter;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.StreamCorruptedException;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.dto.Address;
import com.hmyd.hmyd_android.shop.JfMallAddNewAddressActivity;

public class JfMallAddressAdapter extends BaseAdapter {

	private Context context;

	private List<Address> addressData;

	private AdapterNetRequest adapterNetRequest;

	private Editor editor;
	private SharedPreferences sharedPreferences;

	private Address mdefaultAddress;

	public JfMallAddressAdapter(Context context, List<Address> addressData,
			AdapterNetRequest adapterNetRequest) {
		this.context = context;
		this.addressData = addressData;
		this.adapterNetRequest = adapterNetRequest;
		sharedPreferences = context.getSharedPreferences("test",
				Activity.MODE_PRIVATE);
		editor = sharedPreferences.edit();
		String address = sharedPreferences.getString("address", "");
		getDefaultAddress(address);
	}

	@Override
	public int getCount() {
		return addressData.size();
	}

	@Override
	public Object getItem(int position) {
		return addressData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.view_adress_item, null);
			viewHolder.addressDelete = (Button) convertView
					.findViewById(R.id.addDelete);
			viewHolder.addressEdit = (Button) convertView
					.findViewById(R.id.addEdit);
			viewHolder.defaultAddCheck = (CheckBox) convertView
					.findViewById(R.id.defaultAddCheck);
			viewHolder.recAddress = (TextView) convertView
					.findViewById(R.id.recAdd);
			viewHolder.recFPhone = (TextView) convertView
					.findViewById(R.id.recFPhone);
			viewHolder.recMPhone = (TextView) convertView
					.findViewById(R.id.recMPhone);
			viewHolder.recName = (TextView) convertView
					.findViewById(R.id.recName);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		
		final Address address = addressData.get(position);
		final View tempView = convertView;
		if (mdefaultAddress != null && mdefaultAddress.id.equals(address.id)) {
			viewHolder.defaultAddCheck.setChecked(true);
			tempView.setBackgroundColor(context.getResources().getColor(
					R.color.text_orange));
		} else {
			viewHolder.defaultAddCheck.setChecked(false);
			tempView.setBackgroundColor(context.getResources().getColor(
					R.color.white));
		}

		viewHolder.addressEdit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(context, JfMallAddNewAddressActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				Bundle bundle = new Bundle();
				bundle.putSerializable("address", address);
				intent.putExtras(bundle);
				context.startActivity(intent);
			}
		});

		final CheckBox cb = viewHolder.defaultAddCheck;
		cb.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (cb.isChecked()) {
					adapterNetRequest.defaultNetRequest(address.id);
					getDefaultAddress(sharedPreferences
							.getString("address", ""));
				} else {
					editor.putString("address", "");
					mdefaultAddress = null;
				}
				notifyDataSetChanged();
			}
		});

		viewHolder.addressDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				adapterNetRequest.deleteAddress(address.id);
				addressData.remove(address);
				notifyDataSetChanged();
			}
		});
		viewHolder.recName.setText(address.addressee);
		viewHolder.recMPhone.setText(address.cellPhoneNumber);
		viewHolder.recFPhone.setText(address.fixedTelephone);
		viewHolder.recAddress.setText(address.province + address.city
				+ address.area + address.address);

		return convertView;
	}

	/**
	 * 用于adapter中通过activity请求网络数据的接口
	 * 
	 * @author wangk
	 */
	public interface AdapterNetRequest {
		public void defaultNetRequest(String itemId);

		public void deleteAddress(String itemId);
	}

	public void getDefaultAddress(String defaultAddress) {
		if (!defaultAddress.equals("")) {
			byte[] addressBytes = Base64.decode(defaultAddress.getBytes(),
					Base64.DEFAULT);
			ByteArrayInputStream bais = new ByteArrayInputStream(addressBytes);
			try {
				ObjectInputStream bis = new ObjectInputStream(bais);
				try {
					mdefaultAddress = (Address) bis.readObject();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} finally {
					bis.close();
				}
			} catch (StreamCorruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	class ViewHolder {
		TextView recName; // 接收者
		TextView recMPhone; // 接收手机
		TextView recFPhone; // 接收固话
		TextView recAddress; // 接收地址
		CheckBox defaultAddCheck; // 默认地址
		Button addressEdit; // 编辑地址
		Button addressDelete; // 删除地址
	}

}
